package com.test.daily.leetcode.y2022.m06.day0607.v02;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/6/7 下午 07:50
 * @version: 1.0
 */
public class Solution {

    public int minEatingSpeed(int[] piles, int h) {
        int low = 1;
        int high = 0;
        for (int i = 0; i < piles.length; i++) {
            high = Math.max(high, piles[i]);
        }
        while (low < high) {
            int mid = low + ((high - low) >> 1);
            int total = totalHour(piles, mid);
            if (total > h) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }
        return high;
    }

    public int totalHour(int[] piles, int k) {
        int sum = 0;
        for (int i = 0; i < piles.length; i++) {
            sum += (piles[i] + k - 1) / k;
        }
        return sum;
    }
}
